package jman.competition14;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Comment
 */
public class Comment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Comment() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int passageId=1;
		if (UserService.session.getAttribute("userInfo")==null ) {
			response.sendRedirect("jmanLogin.competition14.html");
		} else {
			Hashtable h=(Hashtable)UserService.session.getAttribute("userInfo");
			String userName=(String) h.get("userName");
			String comment=request.getParameter("comment");
			boolean flag=false;
			UserService us=new UserService();
			flag=us.comment(userName, comment, passageId);
			if(flag){
				response.sendRedirect("jmanCommentSuccess.html");
			}else{
				response.sendRedirect("jmanCommentFail.html");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		CommentDao cd=new CommentDao();
		try {
			ArrayList<CommentAnnal> CommentList=cd.getCommentList();
			request.setAttribute("CommentList",CommentList);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.getRequestDispatcher("index.jsp").forward(request, response);
		doGet(request, response);
	}

}
